package com.zz.cc.owner.service;

import android.os.Binder;
import android.os.IBinder;
import android.os.IInterface;
import android.os.Parcel;
import android.os.RemoteException;

public interface ICcService extends IInterface {

	public static abstract class Stub extends Binder implements ICcService {
		private static final String DESCRIPTOR = ICcService.class.getName();
		
		public Stub() {
			this.attachInterface(this, DESCRIPTOR);
		}
		
		public static ICcService asInterface(IBinder obj) {
			if(null == obj) {
				return null;
			}
			
			IInterface iin = (IInterface) obj.queryLocalInterface(DESCRIPTOR);
			if((null != null) && (iin instanceof ICcService)) {
				return (ICcService) iin;
			} else {
				return new Proxy(obj);
			}
					
		}
		
		@Override
		public IBinder asBinder() {
			return this;
		}
		
		@Override
		public boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException{
			switch(code) {
			case INTERFACE_TRANSACTION: {
				reply.writeString(DESCRIPTOR);
				return true;
			}
				
			case TRANSACTION_LOGIN: {
				data.enforceInterface(DESCRIPTOR);
				String userName = data.readString();
				String pwd = data.readString();
				int result = this.login(userName, pwd);
				reply.writeNoException();
				reply.writeInt(result);
				return true;
			}
				
			case TRANSACTION_LOGOUT: {
				data.enforceInterface(DESCRIPTOR);
				int result = this.logout();
				reply.writeNoException();
				reply.writeInt(result);
				return true;
			}
				
			default:
				break;
			}
			
			return super.onTransact(code, data, reply, flags);
		}
		
		private static class Proxy implements ICcService {
			private IBinder mRemote;
			
			Proxy(IBinder remote) {
				mRemote = remote;
			}
			
			@Override
			public IBinder asBinder() {
				return mRemote;
			}
			
			public String getInterfaceDescriptor() {
				return DESCRIPTOR;
			}

			@Override
			public int login(String userName, String pwd)
					throws RemoteException {
				Parcel data = Parcel.obtain();
				Parcel reply = Parcel.obtain();
				int result;
				try {
					data.writeInterfaceToken(DESCRIPTOR);
					data.writeString(userName);
					data.writeString(pwd);
					mRemote.transact(Stub.TRANSACTION_LOGIN, data, reply, 0);
					reply.readException();
					result = reply.readInt();
				} finally {
					reply.recycle();
					data.recycle();
				}
				return result;
			}

			@Override
			public int logout() throws RemoteException {
				Parcel data = Parcel.obtain();
				Parcel reply = Parcel.obtain();
				int result;
				try {
					data.writeInterfaceToken(DESCRIPTOR);
					mRemote.transact(Stub.TRANSACTION_LOGOUT, data, reply, 0);
					reply.readException();
					result = reply.readInt();
				} finally {
					reply.recycle();
					data.recycle();
				}
				return result;
			}
		}
	}
	
	/*package*/ static final int TRANSACTION_LOGIN = IBinder.FIRST_CALL_TRANSACTION;
	/*package*/ static final int TRANSACTION_LOGOUT = TRANSACTION_LOGIN + 1;
	
	public int login(String userName, String pwd) throws RemoteException;
	public int logout() throws RemoteException;
}
